package com.delaphone.flocash.web.action;

import java.net.UnknownHostException;
import org.jpos.gl.Currency;
import com.delaphone.flocash.model.FCountry;
import com.delaphone.flocash.model.FUser;
import com.delaphone.flocash.web.service.MailService;
import com.delaphone.flocash.service.SystemService;
import com.delaphone.flocash.service.UserService;
import com.delaphone.flocash.util.SecurityUtil;
import com.delaphone.flocash.util.SystemConts;
import com.delaphone.flocash.web.util.Validate;
import com.opensymphony.xwork2.Action;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author dongtv
 */
public class JoinAction extends BaseAction {

    private String email;
    private String password;
    private String confirm_password;
    private String first_name;
    private String last_name;
    private String address;
    private String city;
    private String state;
    private String postal_code;
    private String country;
    private String phone_number;
    private String currency;
    private boolean news_letter;
    private boolean check_terms;
    private MailService mailService;
    private SystemService sysService;
    private ArrayList<FCountry> countries;
    private ArrayList<Currency> currencies;
    private UserService userService;
    private String content;

    /**
     * join input
     * @return 
     */
    public String join() {
        return Action.SUCCESS;
    }

    /**
     * do join
     * @return
     * @throws Exception 
     */
    public String doJoin() throws UnknownHostException {

        validateFields();
        if (getFieldErrors().size() > 0 || getActionErrors().size() > 0) {
            return Action.INPUT;
        }
        FUser newUser = new FUser();
        newUser.setEmail(email);
        newUser.setPassword(password);
        newUser.setAccountType("personal");
        newUser.setActive("no");
        newUser.setFirstName(first_name);
        newUser.setLastName(last_name);
        newUser.setAddress(address);
        newUser.setCity(city);
        newUser.setCountryCode(country);
        newUser.setState(state);
        newUser.setAddressVerified("no");
        newUser.setGluser("system");
        newUser.setCreated(new Date());
        newUser.setPhoneNumber(phone_number);
        newUser.setCurrency(currency);
        newUser.setTxLimit(SystemConts.DEFAULT_TX_LIMIT);
        newUser.setDailyLimit(SystemConts.DEFAULT_DAILY_LIMIT);
        newUser.setDepositLimit(SystemConts.DEFAULT_DEPOSIT_LIMIT);

        newUser.setSuspended("no");
        newUser.setPostalCode(postal_code);
        if (userService.createUser(newUser)) {
            String activation = SecurityUtil.genActivationKey(email);
            System.out.println("Your code active:" + activation);
            sysService.saveUserActivation(email, activation);
            //preapare to send activation email
            String mail_content = null;
            String input[] = {email, activation, context.getInitParameter("baseUrl")};
            mail_content = getText("email.active", input);

            mailService.sendMail(email, getText("email.registration"), mail_content);

            String in[] = {email};
            setContent(" " + getText("email.registration.content", in));
        }

        return Action.SUCCESS;
    }

    /**
     * validate
     */
    public void validateFields() {
        if (Validate.isEmptyString(email)) {
            addFieldError("email", "*");
        }
        if (Validate.isEmptyString(password)) {
            addFieldError("password", "*");
        }
        if (Validate.isEmptyString(confirm_password)) {
            addFieldError("confirm_password", "*");
        }
        if (Validate.isEmptyString(first_name)) {
            addFieldError("first_name", "*");
        }
        if (Validate.isEmptyString(last_name)) {
            addFieldError("last_name", "*");
        }
        if (Validate.isEmptyString(address)) {
            addFieldError("address", "*");
        }
        if (Validate.isEmptyString(state)) {
            addFieldError("state", "*");
        }
        if (Validate.isEmptyString(postal_code)) {
            addFieldError("postal_code", "*");
        }
        if (Validate.isEmptyString(city)) {
            addFieldError("city", "*");
        }
        if (Validate.isEmptyString(phone_number)) {
            addFieldError("phone_number", "*");
        }
        if (Validate.isEmptyString(country)) {
            addFieldError("country", "*");
        }
        if (getFieldErrors().size() > 0) {
            addActionError(getText("join.warn.missing_required"));
        } else {
            if (!check_terms) {
                addActionError(getText("join.warn.accept_term"));
            }
            if (!Validate.isEmail(email)) {
                addActionError(getText("join.warn.email_wrong_format"));
            }
            if (!getPassword().equals(getConfirm_password())) {
                addActionError(getText("join.warn.pwd_mismatch"));
            }
            if (!Validate.isPhoneNumber(phone_number)) {
                addActionError(getText("join.warn.invalid_phone"));
            }

            //check user existance
            if (userService.checkUserExist(email)) {
                addActionError(getText("join.warn.user_existed", new String[]{email}));
            }
        }

    }

// <editor-fold defaultstate="collapsed" desc="set and get method">
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getConfirm_password() {
        return confirm_password;
    }

    public void setConfirm_password(String confirm_password) {
        this.confirm_password = confirm_password;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCurrency() {

        return currency;
    }

    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone_number() {
        return phone_number;
    }

    public void setPhone_number(String phone_number) {
        this.phone_number = phone_number;
    }

    public String getPostal_code() {
        return postal_code;
    }

    public void setPostal_code(String postal_code) {
        this.postal_code = postal_code;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public boolean isCheck_terms() {
        return check_terms;
    }

    public void setCheck_terms(boolean check_terms) {
        this.check_terms = check_terms;
    }

    public boolean isNews_letter() {
        return news_letter;
    }

    public void setNews_letter(boolean news_letter) {
        this.news_letter = news_letter;
    }

    public MailService getMailService() {
        return mailService;
    }

    public void setMailService(MailService mailService) {
        this.mailService = mailService;
    }

    public SystemService getSysService() {
        return sysService;
    }

    public void setSysService(SystemService sysService) {
        this.sysService = sysService;
    }

    public ArrayList<FCountry> getCountries() {
        countries = (ArrayList<FCountry>) sysService.getAllContries();
        return countries;
    }

    public void setCountries(ArrayList<FCountry> countries) {
        this.countries = countries;
    }

    public ArrayList<Currency> getCurrencies() {
        currencies = (ArrayList<Currency>) sysService.getAllCurrencies();
        return currencies;
    }

    public void setCurrencies(ArrayList<Currency> currencies) {
        this.currencies = currencies;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }// </editor-fold>
}
